﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using System.Data;
using System.Text.RegularExpressions;
using System.Text;

namespace Catering.Catering_Page.Mes
{
    public partial class MobileSMS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadPage();
                TreeView1.Attributes.Add("onclick", "OnTreeNodeChecked()");
            }
        }

        private void loadPage()
        {
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            Bind_Data_to_Controls.Bind_Tree_Receiver(TreeView1, pid, true);
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            string msg = this.TextBox1.Text.Trim();
            if (string.IsNullOrEmpty(msg)) return;
            string usertel = this.txt_phoneNo.Text;
            string[] splittel = usertel.Split(';');
            if (splittel.Length > 0)
            {
                foreach (string s in splittel)
                {
                    Match telmatch = Regex.Match(s, @"\([^\)]+\)");
                    usertel = telmatch.Groups.Count > 0 ? telmatch.Groups[0].Value : "";
                    usertel = usertel.TrimStart('(').TrimEnd(')');
                    if (!string.IsNullOrEmpty(usertel))
                    {
                        if (Regex.IsMatch(usertel, @"^((\(\d{3}\))|(\d{3}\-))?13[0-9]\d{8}|15[89]\d{8}"))
                        {
                            Catering_Model.OutBoxInfo outbox = new Catering_Model.OutBoxInfo();
                            outbox.ExpressLevel = 2;
                            //outbox.Sender = "";
                            outbox.ReceiverMobileNo = usertel;
                            outbox.Msg = msg;
                            outbox.SendTime = DateTime.Now;
                            outbox.IsChinese = true;
                            outbox.CommPort = 0;
                            new Catering_Rules.OutBoxInfo().AddOutBoxInfo(outbox);
                        }
                    }
                }
            }
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>if(confirm('是否转到正在发送页面以查看发送状态？')){location.href='MobileSMSQueue.aspx';}else{location.href='MobileSMS.aspx';}</script>");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtSearch.Text))
            {
                string searchText = txtSearch.Text;
                foreach (TreeNode root in TreeView1.Nodes)
                {
                    foreach (TreeNode Parent in root.ChildNodes)
                    {
                        foreach (TreeNode Child in Parent.ChildNodes)
                        {
                            if (Child.Text.Contains(searchText))
                            {
                                Child.Checked = true;
                                Child.Expand();
                                Child.Parent.Expand();
                                Child.Parent.Parent.Expand();
                                break;
                            }
                        }
                    }
                }
            }
        }

        protected void btnSelectRecevie_Click(object sender, EventArgs e)
        {
            string phoneNo = "";
            foreach (TreeNode root in TreeView1.Nodes)
            {
                foreach (TreeNode Parent in root.ChildNodes)
                {
                    foreach (TreeNode Child in Parent.ChildNodes)
                    {
                        if (Child.Checked)
                        {
                            phoneNo += Child.Text + ";";
                        }
                    }
                }
            }
            this.txt_phoneNo.Text = this.txt_phoneNo.Text + phoneNo;
        }

        protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {

        }
    }
}